import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.spi.AudioFileReader;

public class Main {
    /**
     * mp3的字节数组生成wav文件
     * @param sourceBytes
     * @param targetPath
     */

    public static AudioInputStream getAudioInputStream(InputStream stream)
            throws UnsupportedAudioFileException, IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        Method method = AudioSystem.class.getDeclaredMethod("getAudioFileReaders");
        method.setAccessible(true);
        List providers = (List) method.invoke(null);
        AudioInputStream audioStream = null;

        for(int i = 0; i < providers.size(); i++ ) {
            AudioFileReader reader = (AudioFileReader) providers.get(i);
            try {
                audioStream = reader.getAudioInputStream( stream ); // throws IOException
                break;
            } catch (UnsupportedAudioFileException e) {
                continue;
            }
        }

        if( audioStream==null ) {
            throw new UnsupportedAudioFileException("could not get audio input stream from input stream");
        } else {
            return audioStream;
        }
    }

    public static boolean byteToWav(byte[] sourceBytes, String targetPath) throws IOException, UnsupportedAudioFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        if (sourceBytes == null || sourceBytes.length == 0) {
            System.out.println("Illegal Argument passed to this method");
            return false;
        }

        final ByteArrayInputStream bais = new ByteArrayInputStream(sourceBytes);
        final AudioInputStream sourceAIS = getAudioInputStream(bais);
            AudioFormat sourceFormat = sourceAIS.getFormat();
            // 设置MP3的语音格式,并设置16bit
            AudioFormat mp3tFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels() * 2, sourceFormat.getSampleRate(), false);
            // 设置百度语音识别的音频格式
            AudioFormat pcmFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 16000, 16, 1, 2, 16000, false);
                    // 先通过MP3转一次，使音频流能的格式完整
                    final AudioInputStream mp3AIS = AudioSystem.getAudioInputStream(mp3tFormat, sourceAIS);
                    // 转成百度需要的流
                    final AudioInputStream pcmAIS = AudioSystem.getAudioInputStream(pcmFormat, mp3AIS);
                // 根据路径生成wav文件
                AudioSystem.write(pcmAIS, AudioFileFormat.Type.WAVE, new File(targetPath));
            return true;

    }

    /**
     * 将文件转成字节流
     * @param filePath
     * @return
     */
    private static byte[] getBytes(String filePath) throws IOException {
        byte[] buffer = null;
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();

        return buffer;
    }

    public static void main(String args[]) throws IOException, UnsupportedAudioFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        String filePath = "C:\\Users\\zhu\\Documents\\WeChat Files\\zyz19961022\\Files\\1.mp3";
        String targetPath = "C:\\Users\\zhu\\Documents\\WeChat Files\\zyz19961022\\Files\\2.wav";
        byteToWav(getBytes(filePath), targetPath);

    }
}


